package com.shiwaixiangcun.customer.presenter.impl;

import android.content.Context;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;
import com.shiwaixiangcun.customer.GlobalApi;
import com.shiwaixiangcun.customer.GlobalConfig;
import com.shiwaixiangcun.customer.entity.ResponseEntity;
import com.shiwaixiangcun.customer.network.HttpCallBack;
import com.shiwaixiangcun.customer.network.HttpRequest;
import com.shiwaixiangcun.customer.presenter.IGetRentHousePresenter;
import com.shiwaixiangcun.customer.ui.IHouseGetRentView;
import com.shiwaixiangcun.customer.ui.dialog.DialogLoading;
import com.shiwaixiangcun.customer.utils.AppSharePreferenceMgr;
import com.shiwaixiangcun.customer.utils.JsonUtil;
import com.shiwaixiangcun.customer.utils.LoginOutUtil;
import com.shiwaixiangcun.customer.utils.RefreshTokenUtil;

import java.lang.reflect.Type;
import java.util.HashMap;

/**
 * @author Administrator
 * @date 2017/5/25
 */

public class HouseGetRentImpl implements IGetRentHousePresenter {
    private IHouseGetRentView iHouseGetRentView;
    private String strContent;
    private DialogLoading mDialogLoading;

    public HouseGetRentImpl(IHouseGetRentView iHouseGetRentView, String strContent) {
        this.iHouseGetRentView = iHouseGetRentView;
        this.strContent = strContent;
    }

    @Override
    public void setBgaAdpaterAndClick(Context context) {
        mDialogLoading = new DialogLoading(context, "正在提交");
        mDialogLoading.show();
        sendGetRentHttp(context);
    }


    //租房
    private void sendGetRentHttp(final Context context) {

        final String refreshToken = (String) AppSharePreferenceMgr.get(context, GlobalConfig.Refresh_token, "");
        HashMap<String, Object> hashMap = new HashMap<>(2);
        hashMap.put("access_token", AppSharePreferenceMgr.get(context, GlobalConfig.TOKEN, ""));
        hashMap.put("content", strContent);

        HttpRequest.post(GlobalApi.INSTANCE.getGetRent(), hashMap, new HttpCallBack() {
            @Override
            public void onSuccess(String responseJson) {
                Type type = new TypeToken<ResponseEntity>() {
                }.getType();
                ResponseEntity responseEntity = JsonUtil.fromJson(responseJson, ResponseEntity.class);

                if (responseEntity.getResponseCode() == 1001) {
                    iHouseGetRentView.setBgaAdpaterAndClickResult(responseEntity);
                    mDialogLoading.close();
                } else if (responseEntity.getResponseCode() == 1018) {
                    RefreshTokenUtil.refreshToken(context, refreshToken);
                } else if (responseEntity.getResponseCode() == 1019) {
                    LoginOutUtil.sendLoginOutUtil(context);
                } else if (responseEntity.getResponseCode() == 1002) {
                    Toast.makeText(context, responseEntity.getMessage(), Toast.LENGTH_LONG).show();
                    mDialogLoading.close();
                }

            }

            @Override
            public void onFailed(Exception e) {
                Toast.makeText(context, "网络异常，请稍后再试...", Toast.LENGTH_LONG).show();
                mDialogLoading.close();
            }
        });
    }


}
